/*
    This file is part of Raptor.

    Raptor is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Raptor is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Raptor.  If not, see <http://www.gnu.org/licenses/>.

 */

#ifndef XMLUPDATER_H
#define XMLUPDATER_H

#include "include/raptor.h"

#include <QObject>
#include <QFile>
#include <QDir>
#include <QDebug>
#include <QMessageBox>
#include <QUrl>
#include <QHttp>
#include <QDateTime>
#include <QApplication>
#include <QDialog>
#include <QtGui>

#include "polarssl/md5.h"
#include "polarssl/sha2.h"

class QPushButton;
class QTreeView;
class QStandardItemModel;

class updateDialog : public QDialog
{
    Q_OBJECT

    public:

        struct S_UPDATEINFO
        {
            QString version;
            QString name;
            QString url;
            QString platform;

            S_UPDATEINFO(QString const& _version, QString const& _name, QString const& _url, QString const& _platform) : version(_version), name(_name), url(_url), platform(_platform) {};
        };

        updateDialog(Raptor *raptor, QList<S_UPDATEINFO> const& updateInfos);
        ~updateDialog();

    private:

        QPointer<Raptor> r;
        QPointer<QPushButton> okPushButton;
        QPointer<QTreeView> treeView;
        QPointer<QStandardItemModel> modelFiles;

    private slots:
        void slot_okPushButton_clicked();
};

class xmlUpdater : public QObject
{
    Q_OBJECT

    public:

	enum E_TYPE
	{
	    TYPE_HOSTER,
	    TYPE_CONTAINER,
	};

	enum E_FILESTATE
	{
	    STATE_OK,
	    STATE_BAD,
	    STATE_UPDATE,
            STATE_STRANGE,
	    STATE_WAITING,
	    STATE_TIMEOUT,
	    STATE_ERROR,
            STATE_DONE,
	};

        xmlUpdater(bool const& useSSL=true);
	~xmlUpdater();

	void setHosterPath(QString const& path);
	void setContainerPath(QString const& path);
        int setProxy(QNetworkProxy const& proxy);

	void updateHoster();
	void updateContainer();
        void updateRaptor(Raptor *raptor);

    private:

	QDir pathHoster;
	QDir pathContainer;

	QByteArray response;
	E_FILESTATE state;
	int waitingId;

	QHttp http;

        QMap<QString, int> updateLog;

        QUrl raptorUrl;

	int timeout;
        int dlTimeout;
        int maxTries;

        xmlUpdater::E_FILESTATE checkFile(QString const& file, xmlUpdater::E_TYPE const type, QString &reply);
  
        QByteArray getPostArray(QString const& action);

        void updateXmls(xmlUpdater::E_TYPE const type);
	bool updateXml(QFileInfo const& fileInfo, xmlUpdater::E_TYPE const &type);

    private slots:

        void slot_http_requestFinished(int requestId, bool httpError);
        void slot_http_readyRead(const QHttpResponseHeader & resp);
        void slot_http_sslErrors(const QList<QSslError> &errors);
        void slot_http_stateChanged(int _state);

    signals:
        void msg(QString const& msg);
};

#endif // XMLUPDATER_H
